(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:LayoutCtrl
     * @description
     * # LayoutCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('LayoutCtrl', LayoutCtrl);

    LayoutCtrl.$inject = ['$scope', 'dataService', '$timeout', '$location', 'helper', '$document', '$filter', '$state', '$rootScope', '$compile', 'moment', '$sce', '$window'];


    function LayoutCtrl($scope, dataService, $timeout, $location, helper, $document, $filter, $state, $rootScope, $compile, moment, $sce, $window) {

        $scope.includeInit = function(card) {
            if (card) {
                card.type = '1';
            }
            // console.error('1111111111111111include page finish.',card);
        }

        //初始方法
        $scope.listAction = function() {

            if ($window.innerHeight - 110 < 300) {
                $scope.modalHeight = 350;
            } else {
                $scope.modalHeight = $window.innerHeight - 110;
            }
            var centerWidth = ($window.innerWidth - 130 - 245 - 15 - 320 - 155) * 0.4;
            $('#sidebar').css("min-height", ($window.innerHeight - 125) + "px")

            $('.sidebar_ul').css("max-height", $window.innerHeight - 120 + "px");
            $('.sidebar_ul').css("min-height", $window.innerHeight - 105 + "px");
            $('.rightBody').css("padding-bottom", '0px');
            $('.rightDivBody').css("max-height", ($window.innerHeight - 98) + "px");
            $('.rightDivBody').css("min-height", $window.innerHeight - 100 + "px");
            // $('.rightBody').css("padding-right", "0px");
            $('.rightDivBody').height($window.innerHeight - 98 + "px");
            $('.sidebar_ul').height($window.innerHeight - 105 + "px")
            $('#sidebar').height($window.innerHeight - 110 + "px")
                // $('.sidebar_ul').height($window.innerHeight - 115 + "px")
            $('#wxappid').height($window.innerHeight - 181 + "px");
            $('#pageSetting').height($window.innerHeight - 202 + "px");
            $('#pageSetting').width(127 / 1366 * $window.innerWidth + "px");
            $('#pageSettingFu').width(127 / 1366 * $window.innerWidth + "px");
            var w = angular.element($window);
            // w.on('resize', function() {
            //     if ($window.innerHeight - 110 < 300) {
            //         $scope.modalHeight = 350;
            //     } else {
            //         $scope.modalHeight = window.innerHeight - 110;
            //     }
            // });

            $scope.uid = random();
            $scope.adminlayout = $location.search().adminlayout;
            $scope.admin_layout_id = $location.search().admin_layout_id;
            $scope.admin_layouttype = $location.search().layouttype;
            $scope.backgroundColor = '#ffffff';
            $scope.discountList = [{
                    text: "拼团",
                    value: "pintuan",
                }, {
                    text: "打折",
                    value: "dazhe",
                }, {
                    text: "砍价",
                    value: "kanjia",
                }]
                // $scope.storeid = $rootScope.store_id;
                //店铺变化 通知 
            $scope.$on('event:up-store-id', function() {
                minitab();
                if ($scope.wxappid) $scope.getuimod();
            });

            minitab();
            // console.error("$scope.storedata", $scope.storedata, $scope.wxappid);
            $scope.$watch('storeary', function(newValue, oldValue, scope) {
                if (newValue) {
                    $scope.principal_name = newValue.principal_name;
                    $scope.storeid = newValue.store_id;
                    $scope.getuimod();
                }
            }, true);


            if ($scope.adminlayout) {
                $scope.amtab = 1;
            }

            function minitab() {
                if ($rootScope.industryCode == 5) {
                    $scope.mimitab = 1;
                    $scope.layouttype = "ministore";
                } else {
                    $scope.mimitab = 0;
                    $scope.layouttype = $scope.layouttype ? $scope.layouttype : "home";
                    if ($scope.admin_layouttype) {
                        $scope.layouttype = $scope.admin_layouttype;
                    }
                }
            }
        }
        $scope.spliceimg = function(data) {
            console.error("spliceimgspliceimg", data);
            $timeout(function() {
                if (data) {
                    data.value = '';
                }
            })

        }

        $scope.valid = function(newValue, oldValue) {
            // console.log("watch hgOrders :", newValue, oldValue);
        }

        $scope.showPop = function() {
            $scope.beifenname = "备份" + moment().format('YYYY-MM-DDHH:mm:ss');

            $timeout(function() {
                $.alert({
                    backdrop: 'static',
                    keyboard: true,
                    title: '备份模板',
                    body: '<div>备份模板名称: <input ng-model="beifenname" type="text"  ></div>',
                    okBtn: '{{"ok" | translate}}',
                    closeBtn: true,
                    transition: true,
                    // width: 'small',
                    height: "200px",
                    show: function(e) {
                        $compile($(e.currentTarget).contents())($scope);
                        return true;
                    },
                    okHide: function(e) {
                        $timeout(function() {
                            $scope.savebeifen();
                        });
                    },
                });
            })
        }
        $scope.udpanelCallback = function(moder) {
            if (moder) {
                // moder.value = moder.value?$sce.trustAsHtml($sce.trustAsHtml):"";
                // console.error("moder", moder);
                // _.map(moder.value, function(va) {
                //     console.error("va", va);
                //     if (va) {
                //         va.value = va.value;
                //         console.error(" v ", va);
                //     }
                // });
            }
        }

        //保存备份模板
        $scope.savebeifen = function() {
            $scope.newEffect = clone($scope.Effect.content);

            // console.error("$scope.newEffect", $scope.newEffect);
            var saveData = {
                content: {
                    name: $scope.beifenname,
                    content: $scope.newEffect,
                    type: $scope.layouttype,
                    store_id: $scope.storeid,
                    wxapp_id: $scope.wxappid,
                }
            };

            dataService.userlayoutAdd(saveData).then(function(list) {

                if (list.user_layout_id) {

                    return helper.myAlert("success");
                }
            });
        }

        $scope.jiaoyan = function(ik) {
            if (ik == 1) {
                return true;
            }
        }
        $scope.increment = function() {
            // console.log("increment");
            $scope.value = $scope.value + 1;
        };
        $scope.decrement = function() {
            // console.log("right");
            $scope.value = $scope.value - 1;
        };

        $scope.getlist = function() {
            var query = {
                store_id: $scope.storeid,
                app_id: $scope.wxappid,
                name: "UI-MOd-" + $scope.layouttype,
                types: "ui"
            };
            dataService.wxappGet({
                query: query
            }).then(function(list) {
                if (list.wxappcfg) {
                    // console.error("componentloginpage?component_appid",list.wxappcfg);
                    $scope.lists = list.wxappcfg;
                    $scope.total_count = list.total_count;

                }
            });
        }

        $scope.beifensave = function(content) {
            if (content) {
                // console.error("contentd回传的数据", content);
                content = JSON.parse(content);
                checkArr($scope.oldPublics, content);
            }

        }


        //获取ui组件的公共内容
        $scope.getuimod = function() {

            $scope.Modular = {};
            // $scope.testQrCode();
            var query = {};

            if ($scope.principal_name == '个人') {
                query.ctype = "c";
            } else {
                query.ctype = "b";
            }

            if ($scope.layouttype) {
                query.types = $scope.layouttype;
            }
            $timeout(function() {
                $scope.Publics = {};
                $scope.wximg = '';
                $scope.wcid = 0;
                $scope.Effect = {
                    "content": {}
                };

                dataService.uimodsGet({
                    query: query,
                    "page_size": 200
                }).then(function(list) {
                    if (list && list.uimods) {
                        _.map(list.uimods, function(d, k) {
                            d.data = JSON.parse(d.data);
                        });
                        $scope.Publics = clone(list.uimods);
                        $scope.oldPublics = clone(list.uimods);
                        //                         console.error(" 查询后台组件集合数据==", $scope.Publics);
                        var i = _.findIndex($scope.Publics, { subtype: 'topbar' })
                        if (i > -1) {
                            $scope.topBar = $scope.Publics[i];
                            var color_index = _.findIndex($scope.topBar.data, { name: 'color' })
                            if (color_index) {
                                $scope.topBar_color = $scope.topBar.data[color_index].value;
                            }
                        }
                        $scope.createComponents();
                        $scope.xtotal = list.total_count;

                    }
                });
            })

        }

        //上移数据n
        $scope.moveUp = function(kk) {
            moveData(kk, 1, 1)
        };
        //下移数据n
        $scope.moveDown = function(kk) {
            moveData(kk, 1, 2);
        };
        //头部 
        $scope.moveTop = function(kk) {
            // moveTopOrBtmData(0,kk,1);

            moveTopOrBtmData(0, kk, 1);
        }

        //底部
        $scope.moveBtm = function(kk) {
            moveTopOrBtmData($scope.Effect.content.length - 1, kk, 2);
        }

        //特殊情况
        function moveTopOrBtmData(newkey, oldkey, type) {
            var data = clone($scope.Effect.content);
            var oldData = clone($scope.Effect.content[oldkey]);
            var newData = [];
            _.map(data, function(da, key) {
                if (key == newkey && type == 1) {
                    newData.push(oldData);
                }
                if (key !== oldkey) {
                    newData.push(da);
                }

                if (key == newkey && type == 2) {
                    newData.push(oldData);
                }
            });
            if (newkey == 0) {
                $scope.click_item_key = 0;
            } else {
                $scope.click_item_key = newkey;
            }
            $scope.Effect.content = newData;
            $scope.Modular = oldData;
        }

        function moveData(kk, i, type) {
            if (type == 1) {
                var keyi = kk - i;
            }
            if (type == 2) {
                var keyi = kk + i;
            }
            var card = $scope.Effect.content[keyi];
            // console.error("card", card);
            if (card) {
                if (card.subtype == 'pagebackground' || card.subtype == 'topbar' || card.subtype == 'facepayment') {
                    i = i + 1;
                    moveData(kk, i, type);
                } else {
                    var oldData = clone($scope.Effect.content[kk]);
                    $scope.Effect.content[kk] = card;
                    $scope.Effect.content[keyi] = oldData;
                    $scope.click_item_key = keyi;
                    $scope.Modular = oldData;
                }
            }
        }

        //删除数据nam
        $scope.removeCard = function(kk, name) {
            console.error("name", name, kk);
            for (var i = $scope.Publics.length - 1; i >= 0; i--) {
                if ($scope.Publics[i].name == name) {
                    $scope.Publics[i] = $scope.oldPublics[i];
                    $scope.Publics[i].select = 0;
                    // console.error($scope.oldPublics[i], $scope.Publics[i]);
                }
            }
            $scope.Effect.content.splice(kk, 1);
            console.error($scope.Publics);
        };
        //回调方法
        $scope.itemdata = function(items, type, name, stype, subkey) {
            //商品信息是多维数组时
            // console.error("name", name, subkey);
            if (stype == 'value') {
                var saveitem = [];
                if (!items.item_id) {
                    _.map(items, function(item) {
                        // var itemdata = { "title": item.title, "item_id": item.item_id, "img": item.img, "price": item.price, "fare_id": item.fare_id, "note": item.note, "is_incl_postage": item.is_incl_postage };
                        saveitem.push(item);
                    });
                } else {

                    // saveitem = { "title": items.title, "item_id": items.item_id, "img": items.img, "price": items.price, "fare_id": items.fare_id, "note": items.note, "is_incl_postage": items.is_incl_postage };
                    saveitem = items;
                }
            }

            //是单个字符串时
            if (stype == "itemid") {
                var saveitem = items.item_id;
            }
            var arr = stype.split("_");

            _.map($scope.Publics, function(d, k) {
                if (d.name == name) {
                    _.map(d.data, function(isd, isk) {
                        var path = '';
                        if (arr.length > 1 && isd.ctype == arr[1] && subkey == isk) {
                            if (arr[1] == 'item') {
                                path = '/pages/goods/detail/index?item_id=' + items.item_id;
                                isd.cid = items.item_id;
                            }

                            if (arr[1] == 'cat') {
                                path = '/pages/cat/index?cat_id=' + items;
                                isd.cid = items;
                            }
                            if (arr[1] == 'page') {
                                path = '/pages/page/index?page_id=' + items.activity_page_id;
                                isd.cid = items.activity_page_id;
                            }
                            isd.click_path = path;

                        } else {
                            if (isd.type == type && subkey == isk) {
                                isd[stype] = saveitem;
                            }
                        }

                    });
                    // console.log("d.data", d.data);
                }
            });

        }

        //回调方法
        $scope.savepath = function(path, ctype, cid, cover, name, subk) {
            // console.error("data===", path, ctype, cid, cover, name, subk);
            _.map($scope.Publics, function(d, k) {
                if (d.name == name) {
                    _.map(d.data, function(isd, isk) {
                        // console.error("isd", isd,subk,isk);
                        if (subk == isk) {
                            // console.error("subk", subk, isk);
                            isd.ctype = ctype;
                            isd.click_path = path;
                            isd.cid = cid;
                            isd.cover = cover;
                        }
                    });
                }
            });
        }
        $scope.save = function($event) {
            $scope.newEffect = clone($scope.Effect.content);
            _.map($scope.newEffect, function(d, k) {
                d.position = k;
                _.map(d.data, function(isd, isk) {
                    if (isd.type == 'ueditor' || isd.type == 'udpanel') {
                        if (isd.type == 'udpanel') {
                            isd.value = angular.element(document).find('#' + d.name).html();
                            if (isd.value) {
                                isd.value = isd.value.replace(/ng-scope/g, "");
                                isd.value = isd.value.replace(/editing/g, "");
                                isd.value = isd.value.replace(/ng-isolate-scope/g, "");
                            }

                        }
                        if (isd.value) {
                            if (isd.value.length > 0) {
                                isd.value = html2json(isd.value);
                            }

                        }

                    }

                });
            });

            if ($scope.adminlayout) {
                if (!$scope.mbname) {
                    return helper.myAlert("模板类型名称不能为空");
                }

                var saveData = {
                    content: {
                        admin_layout_id: $scope.wcid,
                        name: $scope.mbname,
                        value: $scope.newEffect,
                        page: $scope.layouttype,
                    }
                };

                var post = $scope.wcid ? "adminlayoutUpdate" : "adminlayoutAdd";

            } else {
                var saveData = {
                    store_id: $scope.storeid,
                    app_id: $scope.wxappid,
                    name: "UI-MOd-" + $scope.layouttype,
                    value: $scope.newEffect,
                    types: "ui"
                };
                // console.error("saveData:", saveData)
                var post = $scope.wcid ? "wxappUpdate" : "wxappAdd";
            }

            dataService[post](saveData).then(function(list) {
                if (list.app_id) {
                    helper.myAlert("success");
                    $scope.getuimod();
                }
                if (list.admin_layout_id) {

                    return helper.myAlert("success", "#/adminlayout/list");

                }
            });
        };

        $scope.cat = function(catid) {

        }

        $scope.Checked = function(name) {
            var type = false;
            _.map($scope.Effect.content, function(d, k) {
                if (d) {
                    if (d.name == name) {
                        type = true;
                    }
                }
            });
            return type;
        }

        $scope.wxaacallback = function(id) {
            if (id) {
                // $scope.wxappid = id;
                $scope.getuimod();
            }
        }

        //创建组件 查询数据
        $scope.createComponents = function() {
            var type = 1;
            if (!$scope.adminlayout) {
                currentAuthrizeApp();
                var data = {
                    store_id: $scope.storeid,
                    appid: $scope.wxappid,
                    cid: null,
                    name: "UI-MOd-" + $scope.layouttype,
                }
            } else {
                type = 2;
                var data = {
                    content: {
                        admin_layout_id: $scope.admin_layout_id,
                    }
                };
            }

            $scope.searchStyle = [{
                "img": "https://g-search1.alicdn.com/img/bao/uploaded/i4/imgextra/i2/188620136570582931/TB28ruZgVXXXXbuXXXXXXXXXXXX_!!12368862-0-saturn_solar.jpg_230x230.jpg"
            }, {
                "img": "https://g-search1.alicdn.com/img/bao/uploaded/i4/imgextra/i2/188620136570582931/TB28ruZgVXXXXbuXXXXXXXXXXXX_!!12368862-0-saturn_solar.jpg_230x230.jpg"
            }];
            //获取可选择组件
            var apiAry = {
                "1": "wxappOne",
                "2": "adminlayoutOne",
            }

            if (!$scope.storeid) {
                return false;
            }

            dataService[apiAry[type]](data).then(function(list) {
                if (list) {
                    // console.error("data",list);
                    if (list.wxapp_cfg_id) {
                        $scope.wcid = list.wxapp_cfg_id;
                    } else {
                        $scope.wcid = list.admin_layout_id;
                        $scope.mbname = list.name;
                    }

                    var value = JSON.parse(list.value);
                    _.map(value, function(d, k) {
                        //编辑器 解码
                        _.map(d.data, function(isd, isk) {
                            if (isd.type == 'ueditor' || isd.type == 'udpanel') {
                                if (isd.value) {
                                    isd.value = json2html(isd.value);
                                }
                            }
                        });
                    });

                    var getData = clone(value);
                    // $scope.Effect.content = value;
                    //把用户设置的值，和管理员设置的公共值，组合在一起
                    if ($scope.Publics.length) {
                        checkArr($scope.Publics, getData);
                    }
                } else {
                    $scope.initUiModes();
                }
            });
            // console.error("Publics", $scope.Publics);
            $scope.$watch('Publics', function(newValue, oldValue, scope) {
                // console.error("Publics", newValue,oldValue);

                if (newValue.length > 0) {

                    //用户正确的数据
                    var oldeef = clone($scope.Effect.content);
                    _.map(oldeef, function(d, k) {
                        d.position = k;
                    });
                    oldeef = AryObj(oldeef);
                    $scope.Effect = {
                        "content": []
                    };
                    var ffect = [];
                    $scope.detial_model = {};
                    _.map(newValue, function(da, ke) {
                        if (da.select) {
                            if (oldeef[da.name]) {
                                da.position = oldeef[da.name].position;
                            }
                            ffect.push(da);
                            if ($scope.layouttype == "detail") {
                                $scope.detial_model[da.subtype] = da;
                            }
                        }
                    });

                    $scope.Effect.content = _.sortBy(ffect, function(n) {
                        return n.position
                    });
                    // console.error("Effect===组件数据", $scope.Effect, $scope.detial_model);
                }
            }, true);


            // $scope.kanbanSortOptions = {

            //     itemMoved: function(event) {

            //        console.error("event",event);
            //         event.source.itemScope.modelValue.status = event.dest.sortableScope.$parent.column.name;
            //         console.error("name",event.dest.sortableScope.$parent.column.name);
            //     },

            //     orderChanged: function(event) {
            //         console.error("name",event);
            //         $scope.operationModular(event);
            //     },
            //     containment: '#board'
            // };
        }

        //选中操作的模块
        $scope.operationModular = function(event) {
            if (event.dest) {
                var iskey = event.dest.index;
                var dary = event.dest.sortableScope.modelValue;

                _.map(dary, function(da, ke) {
                    if (ke == iskey) {

                        $scope.Modular = da;
                        // console.error("$scope.Modular", $scope.Modular);
                    }
                });
            }
        }

        $scope.backColr = function(isnaeme, name, cor) {
            // console.error("isnaeme", isnaeme, name, cor);
            if (isnaeme == 'backgroundColor') {
                $scope.backgroundColor = cor;
            }
        }

        $scope.clickModular = function(data, data_key, $event) {
            if (data) {
                if ($event) {
                    var checkbox = $event.target;
                    if (checkbox.checked) {
                        $scope.Modular = data;
                    } else {
                        $scope.Modular = {};
                    }
                } else {
                    $scope.Modular = data;
                }
                if (data_key) {
                    $scope.click_item_key = data_key;
                } else {
                    $scope.click_item_key = $scope.Effect.content.length;
                }
                // console.error($scope.Modular, $scope.Effect.content, data_key, $event);
            }
        }

        $scope.chosedClass = function(obj) {
            if ($scope.Modular !== obj) {
                return obj;
            }
        }

        //初始化加载组件
        $scope.initUiModes = function() {
            _.map($scope.Publics, function(da, ke) {
                if (da.dft == 2) {

                    da.select = 1;

                    $scope.Effect.content.push(da);
                }
            });
        }

        // //获取单个授权wxapp信息
        function currentAuthrizeApp() {
            if ($scope.wxappid) {
                return dataService.wxappAuthorizerOne($scope.storeid, $scope.wxappid).then(function(a) {
                    $scope.wximg = a.avartar_url ? a.avartar_url : "https://img.alicdn.com/imgextra/i3/20841398/TB2Si6IgRDH8KJjy1zeXXXjepXa-20841398.png";
                });
            }

        }

        $scope.position = function(Value) {
            var pxAry = [];
            var nopxary = [];
            _.map(Value, function(da, ke) {
                if (da.position >= 0) {
                    pxAry.push(da);
                } else {
                    nopxary.push(da);
                }
            });
            var newdata = _.sortBy(pxAry, function(n) {
                return n.position
            });

            _.map(nopxary, function(da, ke) {
                newdata.push(da);
            });

            return newdata;
        }

        //对比两个二维数组中相同的一维数组
        function checkArr(arry1, arry2) {
            // console.error("aaaa", arry1, arry2);
            //1 转换为对象数据
            var arr1 = AryObj(arry1);
            var arr2 = AryObj(arry2);
            // console.error("aaaa", arry1, arry2);
            //2 判断key值是不是相同 把有值 的数据value值替换下
            _.map(arr1, function(data, datakey) {
                if (arr2[datakey]) {
                    data.select = 1;
                    if (arr2[datakey].position >= 0) {
                        data.position = arr2[datakey].position;
                    }
                    _.map(data.data, function(isd, isk) {
                        if (arr2[datakey].data[isk]) {
                            isd.value = arr2[datakey].data[isk].value;
                            // if (isd.itemid) {
                            //     isd.itemid = arr2[datakey].data[isk].itemid;
                            // }
                            if (isd.click_type) {
                                isd.click_type = arr2[datakey].data[isk].click_type ? arr2[datakey].data[isk].click_type : isd.click_type;
                                isd.ctype = arr2[datakey].data[isk].ctype ? arr2[datakey].data[isk].ctype : isd.ctype;
                                isd.cid = arr2[datakey].data[isk].cid || 0;
                                isd.click_path = arr2[datakey].data[isk].click_path ? arr2[datakey].data[isk].click_path : isd.click_path;
                            }
                            if (isd.title) {
                                isd.title = arr2[datakey].data[isk].title;
                            }
                        }
                    });
                } else if (data.dft == 2) {
                    data.select = 1;
                }
            });
            //x执行完毕后的正确的数据
            //4 把新生成的值赋给编辑的值
            arr1 = _.toArray(arr1);
            _.map(arr1, function(da, ke) {
                da.data = _.toArray(da.data);
            });
            //用户正确的数据if
            var newEffect = [];
            _.map(arr1, function(da, ke) {
                if (da.select) {
                    newEffect.push(da);
                }
            });
            //排序
            // console.error(newEffect);
            $scope.Effect.content = $scope.position(newEffect);
            // console.error("$c处理完 数据scope.Effect.content", $scope.Effect.content);
        }

        $scope.delvalue = function(data) {
            if (data) {
                // console.log("delvalue", data);
                data.value = {};
            }
        }

        function AryObj(newAry) {
            // console.error("newArynewArynewArynewAry=", newAry);
            var isary = {};
            _.map(newAry, function(da, ke) {
                isary[da.name] = da;
            });

            _.map(isary, function(da1, ke) {
                da1.newdata = {};
                _.map(da1.data, function(a, k) {
                    da1.newdata[a.name] = a;
                });
            });

            _.map(isary, function(da, ke) {
                da.data = clone(da.newdata);
                delete da.newdata;
            });

            // console.error("isary-====", isary);

            return isary;
        }

        //获取体验二维码
        $scope.testQrCode = function() {
            $timeout(function() {
                var scence = "";
                if ($scope.layouttype == "home") {
                    return scence = "HOME";
                }
                if ($scope.layouttype == "detail") {
                    return dataService.itemGet({
                        page_no: 1,
                        page_size: 1,
                        query: {
                            store_id: $scope.store_id,
                            "on_shelves": 1
                        }
                    }).then(function(list) {
                        if (list) {
                            return scence = 'ITEM' + list.items[0].item_id;
                        }
                    });
                }
                if ($scope.layouttype == "cat") {
                    return dataService.categoryGet({
                        query: {
                            store_id: $scope.store_id
                        }
                    }).then(function(list) {
                        if (list.itemcats[0]) {
                            return scence = 'CATT' + list.itemcats[0].cid;
                        }
                    });
                }
                if ($scope.layouttype == "page") {
                    return dataService.activitypageGet({
                        page_no: 1,
                        page_size: 1,
                        query: {
                            app_id: $scope.wxappid
                        }
                    }).then(function(list) {
                        if (list.activity_pages[0]) {
                            return scence = 'PAGE' + list.activity_pages[0].activity_page_id;
                        }
                    });
                }
            }).then(function(scence) {
                dataService.acodeunlimitGet($scope.wxappid, scence, true, "pages/index/index").then(function(qrimage) {
                    $scope.qrimage = "data:image/png;base64," + qrimage;
                });
            });
        }

        $scope.moveUpItem = function(kk, index, items) {
            moveItemData(index, 1, 1, items)
        }

        $scope.moveDownItem = function(kk, index, items) {
                moveItemData(index, 1, 2, items);
            }
            //头部 
        $scope.moveTopItem = function(kk, items) {
            // moveTopOrBtmData(0,kk,1);
            console.error("kk", kk);
            moveTopOrBtmItemData(0, kk, 1, items);
        }

        //底部
        $scope.moveBtmItem = function(kk, items) {
            moveTopOrBtmItemData(items.value.length - 1, kk, 2, items);
        }

        function moveItemData(index, i, type, items) {
            if (type == 1) {
                var keyi = index - i;
            }
            if (type == 2) {
                var keyi = index + i;
            }
            var card = items[keyi];
            if (card) {
                var oldData = clone(items[index]);
                items[index] = card;
                items[keyi] = oldData;
            }
        }
        //特殊情况
        function moveTopOrBtmItemData(newkey, oldkey, type, data1) {
            var data = clone(data1.value);
            var oldData = clone(data1.value[oldkey]);
            var newData = [];
            _.map(data, function(da, key) {
                if (key == newkey && type == 1) {
                    newData.push(oldData);
                }
                if (key !== oldkey) {
                    newData.push(da);
                }

                if (key == newkey && type == 2) {
                    newData.push(oldData);
                }
            });
            data1.value = newData;
        }
        $scope.showData = function(data, child) {
            console.error("data33333333333", data, child);
        }
        $scope.itemShow = function(num, index) {
            var result = false;
            if (Number(num) == 0 || Number(num) > index) {
                result = true;
            }
            return result;
        }
        $scope.setBool = function(obj, value) {
            obj.value = value
        }
        $scope.initHeight = function() {
            $('#board').height($window.innerHeight - 185 + "px");
        }
        $scope.initBodyHeight = function() {
            setTimeout(function() {
                $('#zujianBody').height($window.innerHeight - 239 + "px");
            }, 100)
        }
        $scope.closeZuJian = function() {
            $scope.Modular = {};
        }
    }
})();